//juqery的核心函数  文档加载完毕自动调用
var sx = null;
var sx02 = null;
var id = localStorage.getItem('studentId')
$(function () {
    getData();
})
//选中付费订单信息
var order = {}

//封装ajax 发送请求获取Tree数据
function getData() {
    //ajax 请求数据库，后台返回一个tree需要的数据，及对应的格式
    $.ajax({
        type: 'post',
        url: '/getStudentStandard',
        data: {'studentId': id},
        dataType: 'json',
        success: function (res) {
            getTree(res)
        },
        error: function (e) {
            console.log('请求失败')
        }
    })
}

//封装方法渲染Tree，方便自动更新页面
function getTree(data) {
    layui.use(['tree', 'layer', 'jquery', 'form', 'table'], function () {
        var tree = layui.tree;
        var layer = layui.layer;
        var $ = layui.$;
        var form = layui.form;
        var table = layui.table;


        //监听提交保存按钮
        form.on('submit(*)', function (d) {
            console.log(d)
            //发送ajax修改选择的学生订单信息
            $.get({
                url: '/standard/updateStandardById',
                data: {
                    studentId:d.field.studentId,
                    studentName:d.field.studentName,
                    preferentialPrice: d.field.preferentialPrice,
                    remark: d.field.preferentialPrice.remark,
                    id:table.getData('sx02')[0].id
                },
                dataType: 'json',
                success: function (res) {
                    if (res.state == '200') {
                        layer.msg('修改订单信息成功', {time: 1500, icon: 6}, function () {
                            setTimeout('window.location.reload()', 1000);
                        })
                    } else {
                        layer.msg('修改订单信息失败', {time: 1500, icon: 6}, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        })
                    }
                },
                error: function (e) {
                    console.log('请求失败')
                }
            })
        })

        //监听分期按钮
        form.on('submit(fq)', function (d) {
            console.log(d.field)
            layer.open({
                type: 2,
                title: '选择分期',
                content: 'Staging.html',
                area: ['60%', '60%'],
                //弹出层成功弹出后的回调函数
                success: function (layero, index) {
                    //父页面向子页面传参 -- 向iframe层传数据
                    //获取iframe页面
                    var body = layer.getChildFrame('body', index);

                }
            })
        })

        //实例化渲染树
        //定义一个全局集合，存储需要查询的列表
        var idList = []
        tree.render({
            elem: '#test1',
            id: 'demoId',
            data: data,
            showCheckbox: true,
            oncheck: function (obj) {
                //定义一个集合择取所有选项
                if (obj.checked == true) {
                    var data = obj.data
                    if (!obj.data.children) {
                        //子节点根据id查订单
                        console.log('子节点')
                        idList.push(obj.data.id)
                    } else {
                        // 有子节点
                        $.each(obj.data.children, function (i) {
                            idList.push(obj.data.children[i].id)
                        })
                    }
                } else {
                    if (!obj.data.children) {
                        var index = idList.findIndex(item => {
                            return item == obj.data.id
                        })
                        console.log("需要删除的元素下角标", index)
                        //取消选中删除对应的id
                        idList.splice(index, 1)
                    } else {
                        // 有子节点
                        $.each(obj.data.children, function (i) {
                            var index = idList.findIndex(item => {
                                return item == obj.data.children[i].id
                            })
                            idList.splice(index, 1)
                        })
                    }

                }
                if (idList.length == 0) {
                    sx02.reload({url: '/standard/getStantardArrearageByInfo'})
                } else {
                    console.log("idList",idList)
                    sx02.reload({
                        url: '/standard/getStantardArrearageById',
                        where: {
                            idList: JSON.stringify(idList)
                        },
                        done(d) {
                            total(d), order = d.data
                        }
                    })
                }
            },
        })

        //付费算法
        function total(d) {
            var all = d.data
            var money = {preferentialPrice: 0, actualPrice: 0, haveFees: 0}
            $.each(all, function (i) {
                money.preferentialPrice += all[i].preferentialPrice    //减免计算
                money.actualPrice += all[i].actualPrice //订单总额应收
                money.haveFees += all[i].haveFees //已收费用
            })
            //映射到页面
            $('input[name=preferentialPrice]').val(money.preferentialPrice)
            $('input[name=actualPrice]').val(money.actualPrice)
            $('input[name=haveFees]').val(money.haveFees)
        }


        //实例化渲染欠费表格
        sx = table.render({
            elem: '#demo',
            url: '/standard/getStantardArrearageByInfo',
            height: 200,
            where: {info: localStorage.getItem("studentId")},
            page: {
                layout: ['count', 'prev', 'page', 'next', 'limit'],
                groups: 5,
                limit: 10,
                limits: [3, 5, 10]
            },
            cols: [
                [
                    {field: 'projectId', title: '订单单号', width: 120, align: 'center'},
                    {field: 'projectName', title: '项目名称', width: 120, align: 'center'},
                    {field: 'arrearage', title: '剩余待缴', width: 110, align: 'center'},
                    {field: 'amountReceivable', title: '总金额', width: 120, align: 'center'},
                    {field: 'preferentialPrice', title: '减免金额', width: 100, align: 'center'},
                    {field: 'haveFees', title: '已收金额', width: 122, align: 'center'},
                    // {field: 'remark', title: '备注信息', width: 120, align: 'center'}
                ]
            ],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.count,
                    "data": res.data
                }
            }
        })


        //实例化渲染表格
        sx02 = table.render({
            elem: '#demo02',
            url: '/standard/getStantardByInfo',
            where: {info: localStorage.getItem("studentId")},
            height: 260,
            id: 'sx02',
            page: {
                layout: ['count', 'prev', 'page', 'next', 'limit'],
                groups: 5,
                limit: 10,
                limits: [3, 5, 10]
            },
            cols: [
                [
                    {
                        field: '', title: '序号', width: 80, templet(d) {
                            return d.LAY_INDEX
                        }, align: 'center'
                    },
                    {field: 'projectId', title: '订单单号', width: 140, align: 'center'},
                    {field: 'projectName', title: '项目名称', align: 'center'},
                    {field: 'arrearage', title: '剩余待缴', width: 100, align: 'center'},
                    {field: 'amountReceivable', title: '总金额', width: 100, align: 'center'},
                    {field: 'preferentialPrice', title: '减免金额', width: 100, align: 'center'},
                    {field: 'haveFees', title: '已收金额', width: 100, align: 'center'},
                    {field: 'remark', title: '备注信息', width: 120, align: 'center'}
                ]
            ],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.count,
                    "data": res.data
                }
            },
            done(d) {
            }
        })
    })
}


